<?php
class Rating extends AppModel {
// Define the model and it's associations:
var $name = 'Rating';
var $belongsTo = array(
					'Comment' => array(
						'className' => 'Comment',
						'foreignKey' => 'object_ID',
						),
					'Pothole' => array(
						'className' => 'Pothole',
						'foreignKey' => 'object_ID',
						)
					);
	
	/*
	// Ratings are a function of the number of positive ratings
	// divided by the total number of ratings.
	*/
	public function getRating($obj_id) {
		$positive = $this->find('count', array(
			'conditions' => array(
				'Rating.object_ID' => $obj_id,
				'Rating.rating' => '1')));
		$total = $this->find('count', array(
			'conditions' => array(
				'Rating.object_ID' => $obj_id)));
		($total > 2) ? ($rating = $positive / $total) : ($rating = .5);
		return $rating;
	}
	
	
	/*
	/	countRatings: Counts the number of ratings on 
	/		pages, comments or currently-active potholes
	/		in a given town.
	/	string $object_id: ID of object on which the count
	/		will happen.
	/	string $mode: pothole, comment, page
	*/
	public function countRatings($object_id, $mode = 'pothole') {
		$conditions = array('Rating.object_ID' => $object_id);
		switch($mode) {
			case 'pothole':
				$conditions['Rating.object_id'] = 'Pothole.ID';
				$conditions['Pothole.status NOT LIKE'] = 'Closed';
				break;
			case 'comment':
				$conditions['Rating.object_id'] = 'Comment.ID';
				$conditions['Comment.object_id'] = 'Pothole.ID';
				$conditions['Pothole.status NOT LIKE'] = 'Closed';
				break;
			case 'page':
				$conditions['Rating.object_id'] = 'Page.ID';
				$conditions['Page.status NOT LIKE'] = 'Closed';
				break;
		}
		$count = $this->find('count', array('conditions' => $conditions));
		return $count;
	}
}
?>
